<?php
class App_Category extends Data_Base
{
    protected static $_tagNames = array();

    public static function tagName ($tagId)
    {
        if (empty(self::$_tagNames))
        {
            
            $query = DB::select('id', 'name')
            ->from('tiyu_category')
            ->execute();

            $ret = array();
            foreach ($query as $rs)
            {
                $ret[$rs['id']] = $rs['name'];
            }

            self::$_tagNames = $ret;
        }

        return Arr::get(self::$_tagNames, $tagId, '无');
    }

    public static function options_categories ($type = 'mainCat')
    {
        $query = DB::select('id', 'name')
            ->from('tiyu_category')
            ->order_by('sort', 'asc');

        if (! empty($type))
        {
            $type = strtoupper($type);
            if ($type == 'MAINCAT')
            {
                $query->where('type', '=', $type);
                $query = $query->execute();
            }
            else
            {
                $query = DB::select('a.id', 'a.name', array('b.name', 'pname'))
                    ->from(array('tiyu_category', 'a'))
                    ->join(array('tiyu_category', 'b'), 'LEFT')
                    ->on('a.parentId', '=', 'b.id')
                    ->where('a.type', '=', DB::expr("'SUBCAT'"))
                    ->order_by('a.name', 'asc')
                    ->execute();

                $result = array();
                foreach ($query as $rs)
                {
                    $result[] = array(
                        'id' => $rs['id'],
                        'name' => $rs['pname'] .'-'. $rs['name'],
                    );
                }

                $query = $result;
            }
        }


        $ret = array();
        foreach ($query as $rs)
        {
            $ret[$rs['id']] = $rs['name'];
        }

        return $ret;
    }


    public static function options_sub_categories ()
    {
        return self::options_categories('SUBCAT');
    }

    public static function options_sport_categories ($include_id_zero = TRUE)
    {
        $query = DB::select('id', 'name')
            ->from('tiyu_category')
            ->where('parentId', '=', 3)
            ->order_by('sort', 'asc')
            ->execute();

        if ($include_id_zero)
            $ret = array(0 => '--');
        else
            $ret = array();

        foreach ($query as $rs)
        {
            $ret[$rs['id']] = $rs['name'];
        }

        return $ret;
 
    }

    public static function goods_categories ()
    {
        $ret = self::all_categories(0, 0, TRUE, 3);
        $cats = Arr::get($ret, 3, array());
        return Arr::get($cats, 'child', array());
    }

    public static function sub_categories($parentId)
    {
    	$query=DB::select('id','name','parentId')
    	          ->from('tiyu_category')
    	          ->where('parentId','=',$parentId)
    	          ->execute();
    	$ret=array();
    	foreach($query as $v)
    	{
                //排除顶级分类:自营
                if($v['id']!=$v['parentId'])
    		$ret[$v['id']]=$v['name'];
    	}
    	return $ret;
    }

    public static function all_categories ($offset, $limit, $return_structure = FALSE, $parentNid = 0)
    {
        $query = DB::select()
            ->order_by('type', 'asc')
            ->order_by('sort', 'asc')
            ->from('tiyu_category');

        if ($offset > 0)
            $query->offset($offset);

        if ($limit > 0)
            $query->limit($limit);

        if ($parentNid > 0)
            $query->where('parentId', '=', $parentNid);

        $query = $query->execute()->as_array();

        $ret = array();
        $names = array();
        foreach ($query as $rs)
        {
            $id   = $rs['id'];
            $type = $rs['type'];
            $pid  = $rs['parentId'];

            if ($type == 'MAINCAT')
            {
                $ret[$id] = array(
                    'info' => $rs,
                    'child' => array(),
                );

                $names[$id] = $rs['name'];
            }
            else 
            {
                $ret[$pid]['child'][] = $rs;
            }
        }

        if ($return_structure)
            return $ret;

        $result = array();
        foreach ($ret as $pid => $pinfo)
        {
            foreach ($pinfo['child'] as $child)
            {
                $child['type'] = '子分类';
                $child['parentId'] = Arr::get($names, $child['parentId'], '');

                $result[] = $child;
            }
        }

        return $result;
    }

    public static function info ($nid)
    {
        $detail = self::item('tiyu_gonglue', 'id', $nid);
        if (empty($detail))
            return array();

        return array(
            'name' => $detail['name'],
            'id'   => $detail['id'],
            'image' => $detail['image'],
            'url'    => 'http://'.$_SERVER['HTTP_HOST'].'/m/gonglue/detail/'.$nid,
        );
    }

    public static function detail ($nid)
    {
        $detail = self::item('tiyu_gonglue', 'id', $nid);
        if (empty($detail))
            return array();



        $goodsList = unserialize($detail['serializeGoodsList']);
        $index = 1;
        foreach ($goodsList as & $goods)
        {
            $goods['goodsInfo'] = App_Goods::info ($goods['goodsId']);
            $goods['indexNo']   = $index++;
        }

        $detail['serializeGoodsList'] = $goodsList;

        return $detail;
    }
}
